/**
 * Copyright 2015-2016 eyaomall.cn
 * All rights reserved.
 *
 * @project
 * @author huwenqing
 * @version 1.0
 * @date 2016-03-07
 */
package com.eyaomall.wxmanage.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.apache.commons.lang.StringUtils;

import com.eyaomall.wxmanage.common.enums.CaptchaEnums;
import com.eyaomall.wxmanage.common.enums.MemberCookieEnums;
import com.eyaomall.wxmanage.common.helper.UserHelper;
import com.eyaomall.wxmanage.common.util.StringUtil;
import com.eyaomall.wxmanage.model.po.User;
import com.eyaomall.wxmanage.service.UserService;
import com.lycheeframework.core.cmp.api.http.GsnData;
import com.lycheeframework.core.cmp.api.http.GsnResponse;
import com.lycheeframework.core.common.cookie.CookieManager;


@Controller
@RequestMapping("login")
public class LoginController {
	
	@Autowired
	private UserService userService;
	
	/**
	 * 登录页
	 * 
	 * @return
	 */
	@RequestMapping("")
	public String Index() {

		return "home/login";
	}
	
	/**
	 * 登录事件
	 * 
	 * @param request
	 * @param response
	 * @param account
	 * @param loginPwd
	 * @return
	 */
	@RequestMapping("in")
	public @ResponseBody GsnData login(HttpServletRequest request,HttpServletResponse response, String account, String loginPwd,String code) {

		Map<String, String> result = new HashMap<String,String>();
		try {
			
			if (StringUtils.isEmpty(account) || StringUtils.isEmpty(loginPwd)) {
				result.put("n", "null");
			}
			//校验验证码
			CookieManager manager = new CookieManager(request,CaptchaEnums._CAPTCHA_FORM.getName());
			String cookieCode = manager.getValue(CaptchaEnums._CAPTCHA_FORM.getName(), CaptchaEnums._CAPTCHA_CODE.getName());
			if(StringUtil.isNotEmpty(code) && !code.equalsIgnoreCase(cookieCode)){
				result.put("flag", "codeError");
				return GsnResponse.toData(result);
			}

			User user = userService.findUserByAccountAndPasswd(account,UserHelper.getEntryPasswd(loginPwd));//UserHelper.getEntryPasswd(loginPwd)

			if (user == null) {
				result.put("flag", "null");
			}

			CookieManager cookie = new CookieManager(request);
			
			cookie.setValue(MemberCookieEnums._MALL_FORM.getName(),
					MemberCookieEnums._MALL_FORM_MEMBER_ID.getName(),
					String.valueOf(user.getId()), true);
			cookie.setValue(MemberCookieEnums._MALL_FORM.getName(),
					MemberCookieEnums._MALL_FORM_PHONE.getName(),
					String.valueOf(user.getPhone()), true);
			cookie.setValue(MemberCookieEnums._MALL_FORM.getName(),
					MemberCookieEnums._MALL_FORM_ACCOUNT.getName(),
					String.valueOf(user.getAccount()), true);
			cookie.setValue(MemberCookieEnums._MALL_FORM.getName(),
					MemberCookieEnums._MALL_FORM_NICKNAME.getName(),
					String.valueOf(user.getNickName()), true);

			cookie.save(response, MemberCookieEnums._MALL_FORM.getName(), true);
			
			result.put("flag", "success");
			return GsnResponse.toData(result);

		} catch (Exception e) {
			result.put("flag", "null");
			return GsnResponse.toData(result);
		}

	}
}
